package cn.fjc920.siaa.activityutils

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate

open class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ActivityCollector.addActivity(this)
        setupTheme()
    }

    open fun setupTheme() {
        val theme = getSharedPreferences("cn.fjc920.siaa_preferences", Context.MODE_PRIVATE)
            .getString("theme","")!!
        if (theme.isNotEmpty()) {
            val provider = ThemeProvider(this)
            val newTheme = provider.getTheme(theme)
            AppCompatDelegate.setDefaultNightMode(newTheme)
        }
    }

    open fun isDarkTheme(context: Context): Boolean {
        val flag = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
        return flag == Configuration.UI_MODE_NIGHT_YES
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }
}